//
//  LXRecommendViewModel.swift
//  zhibo
//
//  Created by Echo on 16/9/27.
//  Copyright © 2016年 Echo. All rights reserved.
//

import UIKit

class LXRecommendViewModel {
    lazy var anchorGroups : [LXAnchorGroup] = [LXAnchorGroup]()
    lazy var circleModels : [LXCircleModel] = [LXCircleModel]()
    private lazy var bigDataGroup : LXAnchorGroup = LXAnchorGroup()
    private lazy var prettyGroup : LXAnchorGroup = LXAnchorGroup()
}

extension LXRecommendViewModel {
    func requestData(finishedCallback : () -> ()) {
        
        let params = ["limit":"4","offset":"0","time":NSDate.getCurrentTime()]
        
        // 创建group,为了保证异步请求下来的数据同时展示
        let dGroup = dispatch_group_create()
        
        // 请求第一部分推荐数据
        dispatch_group_enter(dGroup)
        /// 最新
        //http://capi.douyucdn.cn/api/v1/getbigDataRoom?time=1474968250
        LXNetworkTool.requestData(.GET, URLString: "http://capi.douyucdn.cn/api/v1/getbigDataRoom", params: ["time":NSDate.getCurrentTime()]) { (result) in
    
            guard let resultDict = result as? [String : NSObject] else {return}
            
            guard let resultArr = resultDict["data"] as? [[String : NSObject]] else {return}
            
            self.bigDataGroup.tag_name = "热门"
            self.bigDataGroup.icon_name = "home_header_hot"
            
            for dict in resultArr {
                let anchor = LXAnchor(dict: dict)
                self.bigDataGroup.anchors.append(anchor)
            }
            
            // 离开组
            dispatch_group_leave(dGroup)
        }
        
        /// 颜值
        dispatch_group_enter(dGroup)
        LXNetworkTool.requestData(.GET, URLString: "http://capi.douyucdn.cn/api/v1/getVerticalRoom", params: params) { (result) in
            guard let resultDict = result as? [String : NSObject] else {return}
            
            guard let dataDict = resultDict["data"] as? [[String : NSObject]] else {return}
            
            // 遍历字典，转成模型对象
            // 设置组的属性
            self.prettyGroup.tag_name = "颜值"
            self.prettyGroup.icon_name = "home_header_phone"
            
            // 获取组里的主播数据
            for dict in dataDict {
                let pretty = LXAnchor(dict : dict)
                self.prettyGroup.anchors.append(pretty)
            }
            
            dispatch_group_leave(dGroup)
        }
        
        
        /// 热门数据
        //http://capi.douyucdn.cn/api/v1/getHotCate?limit=4&offset=0&time=1474968250
        
        dispatch_group_enter(dGroup)
        LXNetworkTool.requestData(.GET, URLString: "http://capi.douyucdn.cn/api/v1/getHotCate", params: params) { (result) in
//            print(result)
//            let time = NSDate.getCurrentTime()
//            print(time)
            
            // 将result 由anyobject转成字典先
            guard let resultDict = result as? [String : NSObject] else {return}
            
            // 
            guard let dataArr = resultDict["data"] as? [[String : NSObject]] else {return}
           
            // 遍历数组，获取字典 字典转成模型对象
            for dict in dataArr {
                let group = LXAnchorGroup(dict: dict)
                self.anchorGroups.append(group)
            }
            
//            for group in self.anchorGroups {
//                print(group.tag_name)
////                //英雄联盟
////                炉石传说
////                DOTA2
////                企鹅直播
////                穿越火线
////                魔兽世界
////                守望先锋
////                星际争霸
////                格斗游戏
////                阴阳师
//            }
            
//            for group in self.anchorGroups {
//                for anchor in group.anchors {
////                    print(anchor.nickname)
//                }
//            }
            
            dispatch_group_leave(dGroup)
        }
        
        // *** 所有的数据都请求到，进行排序 ***
        dispatch_group_notify(dGroup, dispatch_get_main_queue()) {
            self.anchorGroups.insert(self.prettyGroup, atIndex: 0)
            self.anchorGroups.insert(self.bigDataGroup, atIndex: 0)
            
            finishedCallback()
        }
    }
}

extension LXRecommendViewModel {
    func requestCircleData(finishedCallback : () -> ()) {
        LXNetworkTool.requestData(.GET, URLString: "http://www.douyutv.com/api/v1/slide/6", params: ["version":"2.300"]) { (result) in
            
            guard let resultDict = result as? [String : NSObject] else {return}
            
//            print(resultDict)
            
            guard let dataArray = resultDict["data"] as? [[String : NSObject]] else {return}
            
            for dict in dataArray {
                let circle = LXCircleModel(dict: dict)
                self.circleModels.append(circle)
            }
            
            finishedCallback()
        }
    }
}

